package com.example.oldkingdom.activities;

import java.util.ArrayList;

import com.example.oldkingdom.R;
import com.example.oldkingdom.areas.areas;
import com.example.oldkingdom.areas.mapArea;
import com.example.oldkingdom.base.worldPoint;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class Explore extends Activity implements OnClickListener, OnEditorActionListener
{
	public static Explore Instance;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
	    Instance = this;
	    CurrentArea = areas.getInstance().getArea(new worldPoint());
		super.onCreate(savedInstanceState);
		setContentView(R.layout.explore);
		View northButton = findViewById(R.id.north_button);
		northButton.setOnClickListener(this);
		View southButton = findViewById(R.id.south_button);
		southButton.setOnClickListener(this);
		View eastButton = findViewById(R.id.east_button);
		eastButton.setOnClickListener(this);
		View westButton = findViewById(R.id.west_button);
		westButton.setOnClickListener(this);
		EditText inputBox = (EditText)findViewById(R.id.input_box);
		inputBox.setOnEditorActionListener(this);
	}
	public void goDirection(worldPoint wp)
	{
		mapArea next = areas.getInstance().getArea(CurrentArea.WorldPoint.plus(wp));
		if (next != null)
		{
		     CurrentArea.leaveArea();
			 CurrentArea = next;
			 CurrentArea.enterArea();
		}
	}
	public void setBackground(Drawable _drawable)
	{
		LinearLayout l = (LinearLayout)findViewById(R.id.explore_background);
		l.setBackground(_drawable);
	}
	public void addDescription(String s)
	{
		while (descriptions.size() > max)
			descriptions.remove(0);
		
		descriptions.add(s);
		
		String wholeDescription = "";
		for (int i = 0; i < descriptions.size();i++)
		{
			wholeDescription += descriptions.get(i) + "\n";
		}
		
		TextView tv = (TextView) Explore.Instance.findViewById(R.id.explore_text);
		tv.setText(wholeDescription);
	}
	private ArrayList<String> descriptions = new ArrayList<String>();
	private int max = 10;
    public mapArea CurrentArea = null;
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) 
	{
		switch (v.getId())
		{
			case R.id.east_button:
				Explore.Instance.goDirection(worldPoint.East);
				break;
			case R.id.west_button:
				Explore.Instance.goDirection(worldPoint.West);
				break;
			case R.id.north_button:
				Explore.Instance.goDirection(worldPoint.North);
				break;
			case R.id.south_button:
				Explore.Instance.goDirection(worldPoint.South);
				break;
		}
		// TODO Auto-generated method stub
	}
	@Override
	public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) 
	{
		if (arg2.getKeyCode() == KeyEvent.KEYCODE_ENTER && arg2.getAction() == KeyEvent.ACTION_DOWN)
		{
			String text = arg0.getText().toString();
			arg0.setText("");
			if (text.equals("look"))
			{
					look();
					return true;
			}
			else if (text == "inv")
			{
				inventory();
				return true;
			}
			else if (text.indexOf("get") == 0)
			{
				get();
			}
			else
			{
				addDescription("You can't do that");
				addDescription("");
			}
		}
		// TODO Auto-generated method stub
		return false;
	}
	public void look()
	{
		CurrentArea.look();
	}
	public void inventory()
	{
	//	addDescription(s);
	}
	public void get()
	{
		addDescription("Getting not yet implemented");
		/*
		addDescription("You attempt to get...");
		Intent i = new Intent(this, GetItemActivity.class);
		startActivity(i);
		*/
	}
}
